import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { options } from '../monitor/mynews/XssWhiteList';
import { codeNameInfoListAll, getFilterOptions } from '/@/api/sys/menu';
import { Input } from 'ant-design-vue';
import { h } from 'vue';
import { ApiSelect } from '/@/components/Form';

export const columns: BasicColumn[] = [
    {
        title: 'guid',
        dataIndex: 'guid'
    },
    {
        title: 'tag',
        dataIndex: 'tag'
    },
    {
        title: 'path',
        dataIndex: 'path'
    },
    {
        title: 'name',
        dataIndex: 'name'
    },
    {
        title: '（1 空间节点   2设备节点   3 点位节点）',
        dataIndex: 'nodeType'
    },
    {
        title: '删除状态',
        dataIndex: 'delFlag'
    },
    {
        title: '设备类型',
        dataIndex: 'deviceType'
    },
    {
        title: '上联设备guid。如有多个上联的，guid间用英文逗号,隔开 ',
        dataIndex: 'link'
    },
    {
        title: 'parentGuid',
        dataIndex: 'parentGuid'
    },
    {
        title: '映射设备类型',
        dataIndex: 'mpDeviceType'
    },
    {
        title: '映射设备大类',
        dataIndex: 'mpBigType'
    },

];

export const searchFormSchema: FormSchema[] = [
    {
        label: 'guid',
        field: 'guid',
        component: 'Input'
    },
    {
        label: 'tag',
        field: 'tag',
        component: 'Input'
    },
];

export const formSchema: FormSchema[] = [
    // TODO 主键隐藏字段，目前写死为ID
    { label: '', field: 'id', component: 'Input', show: false },
    {
        label: 'guid',
        field: 'guid',
        component: 'Input',
        show: false
    },
    {
        label: 'tag',
        field: 'tag',
        component: 'Input',
        show: false
    },
    {
        label: 'path',
        field: 'path',
        component: 'Input',
        show: false
    },
    {
        label: 'name',
        field: 'name',
        component: 'Input',
    },
    {
        label: '（1 空间节点   2设备节点   3 点位节点）',
        field: 'nodeType',
        component: 'InputNumber',
        show: false
    },
    {
        label: '删除状态',
        field: 'delFlag',
        component: 'InputNumber',
        show: false
    },
    {
        label: '设备类型',
        field: 'deviceType',
        component: 'Input',
        show: false
    },
    {
        label: '上联设备guid。如有多个上联的，guid间用英文逗号,隔开 ',
        field: 'link',
        component: 'Input',
        show: false
    },
    {
        label: 'parentGuid',
        field: 'parentGuid',
        component: 'Input',
        show: false
    },
    {
        label: '设备大类',
        field: 'mpBigType',
        component: 'ApiSelect',
        render: ({ model, field }) => {
            //渲染自定义组件，以Input为例
            return h(ApiSelect, {
                api: () => {
                    return getFilterOptions(1) as any
                },
                resultField: 'result',
                labelField: 'mpBigType',
                valueField: 'mpBigType',
                allowClear: true,
                showSearch: true,
                value: model.mpBigType,
                onChange: (e: ChangeEvent) => {
                    if (e == model.mpBigType) {
                        return
                    }
                    model.mpBigType = e
                    model.mpDeviceType = ''
                },
            });
        },
    },
    {
        label: '设备类型',
        field: 'mpDeviceType',
        component: 'ApiSelect',
        render: ({ model, field }) => {
            //渲染自定义组件，以Input为例
            return h(ApiSelect, {
                api: () => {
                    return getFilterOptions(2, (model.mpBigType instanceof Array) ? model.mpBigType.join("") : model.mpBigType) as any
                },
                resultField: 'result',
                labelField: 'mpDeviceType',
                allowClear: true,
                valueField: 'mpDeviceType',
                showSearch: true,
                value: model.mpDeviceType,
                onChange: (e: ChangeEvent) => {
                    model.mpDeviceType = e
                    // model[field] = e.target.value;
                },
                advancedChange: (e: ChangeEvent) => {
                    model.mpDeviceType = e
                    // model[field] = e.target.value;
                }
            });
        },
    },
      {
            label: '机房名称',
            field: 'roomName',
            show: ({ values }) => {
                const type = values.type;
                return  true
            },
            component: 'ApiSelect',
            render: ({ model, field }) => {
                //渲染自定义组件，以Input为例
                return h(ApiSelect, {
                    api: (e) => {
                        let type = 3
                      
                        return codeNameInfoListAll(type,model.searchValue) as any
                    },
                    allowClear: true,
                    resultField: 'result',
                    labelField: 'nameAndCode',
                    valueField: 'nameAndCode',
                    showSearch: true,
                    value: model.roomName,
                    onSearch:(e)=>{
                        model.searchValue = e
                    },
                    onChange: (e: ChangeEvent) => {
                        if (e == model.mpBigType) {
                            return
                        }
                        model.roomName = e.split("-")[0]
                        model.roomid = e.split("-")[e.split("-").length - 1]
                    },
                });
            },
        },
        {
            label: '机房编号',
            field: 'roomid',
            show: ({ values }) => {
                const type = values.type;
                return (type == 1 || type == 2 || type == 3 || type == 4 || type == 6) ? false : true
            },
            component: 'Input',
            componentProps:{
                disabled:true
            }
        },
    
    {
        label: '设备名称',
        field: 'deviceName',
        component: 'ApiSelect',
        render: ({ model, field }) => {
            //渲染自定义组件，以Input为例
            return h(ApiSelect, {
                api: (e) => {
                    return codeNameInfoListAll(4, model.searchValue) as any
                },
                allowClear: true,
                resultField: 'result',
                labelField: 'nameAndCode',
                valueField: 'nameAndCode',
                showSearch: true,
                value: model.deviceName,
                onSearch: (e) => {
                    model.searchValue = e
                },
                onChange: (e: ChangeEvent) => {
                    if (e == model.mpBigType) {
                        return
                    }
                // 方法 2：使用 pop()
                let partsCopy = e.split("-");
                model.deviceName = partsCopy[0];
                model.deviceid = partsCopy.pop();
                },
            });
        },
    },
    {
        label: '设备编号',
        field: 'deviceid',
        component: 'Input',
        componentProps: {
            disabled: true
        }
    },
];
